#include "socket_description.h"

#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

SocketDescription::SocketDescription(int description)
{
    m_socket_description = description;
}

SocketDescription::SocketDescription(Type mode)
{
    m_socket_description = -1;
    m_socket_type        = mode;
}

SocketDescription::~SocketDescription()
{
    if (m_socket_description >= 0)
    {
        close(m_socket_description);
        m_socket_description = -1;
    }
}

int SocketDescription::DescriptionData()
{
    return m_socket_description;
}

int SocketDescription::CreateDescription()
{
    int result             = 0;
    int socket_description = 0;
    if (m_socket_type == TCPSOCKET)
    {
        socket_description = socket(AF_INET, SOCK_STREAM, 0);
    }
    else
    {
        socket_description = socket(AF_INET, SOCK_DGRAM, 0);
    }
    if (socket_description >= 0)
    {
        m_socket_description = socket_description;
        result               = 1;
    }
    else
    {
        result = 0;
    }
    return result;
}

void SocketDescription::CloseDescription()
{
    if (m_socket_description >= 0)
    {
        close(m_socket_description);
        m_socket_description = -1;
    }
}
